﻿// ***************************************************************************
// MapLook
// Copyright (C) 2009 Christian Schmiljun
// http://maplook.codeplex.com
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// ***************************************************************************


using System;
using System.Collections.Generic;
using System.Text;

using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;

namespace MapLook2007
{
    public partial class MapContactExtender
    {

        private string homeAddress;
        private string businessAddress;


        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            this.Application.ItemContextMenuDisplay += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            //throw new NotImplementedException();
        }

        private void Application_ItemContextMenuDisplay(Microsoft.Office.Core.CommandBar CommandBar, Microsoft.Office.Interop.Outlook.Selection Selection)
        {
            foreach (var item in Selection)
            {
                if (item is Outlook.ContactItem)
                {
                    Office.CommandBarPopup btn = (Office.CommandBarPopup)
                        CommandBar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, missing, false);
                    btn.Caption = global::MapLook2007.Resource.str_btn1;                                           

                    Office.CommandBarButton btnBusinessAddress = (Office.CommandBarButton)
                        btn.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, false);
                    btnBusinessAddress.Caption = global::MapLook2007.Resource.str_btn2;

                    btnBusinessAddress.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
                    btnBusinessAddress.Picture = getImage();
                    btnBusinessAddress.Mask = ConvertImage.Convert(Properties.Resources.websearch_mask1);

                    Office.CommandBarButton btnHomeAddress = (Office.CommandBarButton)
                        btn.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, false);
                    btnHomeAddress.Caption = global::MapLook2007.Resource.str_btn3;

                    btnHomeAddress.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;                   
                    btnHomeAddress.Picture = getImage();
                    btnHomeAddress.Mask = ConvertImage.Convert(Properties.Resources.websearch_mask1);

                    btnHomeAddress.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(btnShowMapHome_Click);
                    btnBusinessAddress.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(btnShowMapBusiness_Click);

                    Outlook.ContactItem contact = (Outlook.ContactItem)item;

                    homeAddress = null;
                    if (contact.HomeAddressPostalCode != null)
                    {
                        homeAddress += contact.HomeAddressPostalCode + " ";
                    }
                    if (contact.HomeAddressCity != null)
                    {
                        homeAddress += contact.HomeAddressCity + ", ";
                    }
                    if (contact.HomeAddressStreet != null)
                    {
                        homeAddress += contact.HomeAddressStreet;
                    }
                    businessAddress = null;
                    if (contact.BusinessAddressPostalCode != null)
                    {
                        businessAddress += contact.BusinessAddressPostalCode + " ";
                    }
                    if (contact.BusinessAddressCity != null)
                    {
                        businessAddress += contact.BusinessAddressCity + ", ";
                    }
                    if (contact.BusinessAddressStreet != null)
                    {
                        businessAddress += contact.BusinessAddressStreet;
                    }

                    btnHomeAddress.Enabled = (homeAddress != null);
                    btnBusinessAddress.Enabled = (businessAddress != null);                                   
                }
            }
        }

        void btnShowMapHome_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            ShowMap(homeAddress);
        }

        void btnShowMapBusiness_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            ShowMap(businessAddress);
        }

        void ShowMap(string address)
        {            
            FormMap mapForm = new FormMap();
            mapForm.Show();
            mapForm.SetLocationByDescription(address); 
        }

        private stdole.IPictureDisp getImage()
        {
            stdole.IPictureDisp tempImage = null;
            try
            {
                System.Drawing.Icon newIcon =
                    Properties.Resources.websearch;

                ImageList newImageList = new ImageList();
                newImageList.Images.Add(newIcon);                
                tempImage = ConvertImage.Convert(newImageList.Images[0]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return tempImage;
        }


        #region Von VSTO generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }

    sealed public class ConvertImage : System.Windows.Forms.AxHost
    {
        private ConvertImage()
            : base(null)
        {
        }

        public static stdole.IPictureDisp Convert
            (System.Drawing.Image image)
        {
            return (stdole.IPictureDisp)System.
                Windows.Forms.AxHost
                .GetIPictureDispFromPicture(image);
        }
    }

}
